package net.alche.al.util.alches;

import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.util.IdUtil;
import net.alche.al.util.Far;

import java.io.IOException;
import java.io.InputStream;

//
// @title ToolSystem
// @decsript
// @author yaoliwu
// @date 2018/11/8 21:30
// @version 1.0
//
public class ToolSystem {

  //
  // 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等.
  //
  public static String getOSName() {
    return System.getProperty("os.name").toLowerCase();
  }

  //
  // 获取CPU序列号
  //
  // @return
  //
  public static String getCPUID_Windows() {
    String result = "";
    try {
      File file = File.createTempFile("tmp", ".vbs");
      file.deleteOnExit();
      FileWriter fw = new FileWriter(file);
      String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
          + "Set colItems = objWMIService.ExecQuery _ \n" + "   (\"Select * from Win32_Processor\") \n"
          + "For Each objItem in colItems \n" + "    Wscript.Echo objItem.ProcessorId \n"
          + "    exit for  ' do the first cpu only! \n" + "Next \n";
      fw.write(vbs);
      fw.close();
      Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
      BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line = input.readLine();
      while (line != null) {
        result += line;
        line = input.readLine();
      }
      input.close();
      file.delete();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result.trim();
  }

  public static String getCPUID_linux() throws Exception {
    String result = "";
    String CPU_ID_CMD = "dmidecode";
    BufferedReader bufferedReader = null;
    Process p = null;
    try {
      p = Runtime.getRuntime().exec(new String[] { "sh", "-c", CPU_ID_CMD });
      bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line = null;
      int index = -1;
      line = bufferedReader.readLine();
      while (line != null) {
        index = line.toLowerCase().indexOf("uuid");
        if (index >= 0) {
          result = line.substring(index + "uuid".length() + 1).trim();
          break;
        }
        line = bufferedReader.readLine();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return result.trim();
  }

  public static String getCPUId() {
    try {
      String os = getOSName();
      String cpuId = "";
      if (os.startsWith("windows")) {
        cpuId = ToolSystem.getCPUID_Windows();
      } else if (os.startsWith("linux")) {
        cpuId = ToolSystem.getCPUID_linux();
      }
      if (cpuId == null || "".equals(cpuId)) {
        cpuId = "null";
      }
      return cpuId;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  //
  // 获取classes根目录path(末尾带"/"号)
  //
  // @return
  //
  public String getClassPath() {
    return Thread.currentThread().getContextClassLoader().getResource("").getPath();
  }

  /**
   * 当时是jar运行模式
   * 
   * @return
   */
  public boolean isRunJar() {
    String runType = String.valueOf(ToolSystem.class.getResource("ToolSystem.class"));
    if (runType != null && runType.startsWith("jar:")) {
      return true;
    }
    return false;
  }

  public InputStream getClassPathRes(String resPath) {
    return Thread.currentThread().getContextClassLoader().getResourceAsStream(resPath);
  }

  public static void main222(String[] args) throws Exception {
    ToolSystem tool = new ToolSystem();
    System.out.println(tool.getClassPath());
  }

  public static long currentTimeMillis() {
    return System.currentTimeMillis();
  }

  public static void logRunTime(long beginMillies) {
    Far.log("run ms:", System.currentTimeMillis() - beginMillies);
  }

  public static String getUUid() {
    return IdUtil.simpleUUID();
  }
}